package com.p2p.backend.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.fastjson.JSONObject;
import com.p2p.backend.model.BackendWithdraw;
import com.p2p.backend.service.BackWithdrawService;
import com.p2p.backend.service.ExportService;
import com.p2p.backend.service.OplogService;
import com.p2p.backend.util.PageUtils;
import com.p2p.base.annotation.PermissionNeed;
import com.p2p.base.model.Action;
import com.p2p.base.model.Module;
import com.p2p.base.model.Money;
import com.p2p.base.model.Oplog;
import com.p2p.base.page.PageVo;
import com.p2p.base.web.controller.BaseController;

/**
 * leiwen@yiqihao.com
 */
@RequestMapping("withdraw")
@Controller
public class WithdrawController extends BaseController {

	@Resource
	private BackWithdrawService withdrawService;
	
	@Resource
	private ExportService exportService;


	@PermissionNeed("withdraw_list")
	@RequestMapping("list")
	public void list(PageVo form, Model model) {
		form.handleSearch();
		PageVo page = withdrawService.listPage(form);
		form.resetSearch();
		model.addAttribute("res", 1);
		Map<String,Object> data = PageUtils.data(page);
		model.addAttribute("data", data);
		List<Map<String,Object>> footer = new ArrayList<Map<String,Object>>();
		data.put("footer", footer);
		Map<String,Object> footerOne =new HashMap<String,Object>();
		footer.add(footerOne);
		if(page.getTotalCount()>0){
			Map<String,Object> parames = new HashMap<String,Object>();
			parames.put("startData", form.getStartDate());
			parames.put("endData", form.getEndDate());
			Money amount = withdrawService.sumForCondition(parames);
			footerOne.put("uid", "成功金额");
			footerOne.put("money", amount.getCent());
			
		}else{
			footerOne.put("uid", "成功金额");
			footerOne.put("money", 0);
		}
		footerOne.put("name", "总笔数");
		footerOne.put("bank", page.getTotalCount());
	}

	@PermissionNeed("withdraw_management")
	@RequestMapping("fail")
	public void fail(HttpServletRequest request, @RequestParam Integer id,
			@RequestParam Long uid, ModelMap model) {
		String ip = getRemoteIp(request);
		withdrawService.fail(id, ip, "", uid);
		model.addAttribute("res", 1);
	}

	@PermissionNeed("withdraw_management")
	@RequestMapping("succeed")
	public void succeed(HttpServletRequest request, @RequestParam Integer id,
			@RequestParam Long uid, Model model) {
		String ip = getRemoteIp(request);
		BackendWithdraw withdraw=withdrawService.get(id);
		Oplog oplog = super.oplog(request);
		oplog.setModule(Module.FUNDS_WITHDRAW);
		oplog.setAction(Action.WITHDRAW_SUCCEED);
		oplog.setParams("id="+id);
		if(withdraw.getStatus() == 31){
			model.addAttribute("res", 0);
			oplog.setRemark("failed");
		}else{
			withdrawService.succeed(id, ip, uid);
			model.addAttribute("res", 1);
			oplog.setRemark("ok");
		}
		OplogService.log(oplog);
	}
	
	@RequestMapping("/doExport")
	public void doExportSuccese(PageVo form, HttpServletResponse response) {

		// JSONObject json = new JSONObject();
		if (StringUtils.isBlank(form.getStartDate())
				|| StringUtils.isBlank(form.getEndDate())) {
			this.returnMessage(response, "开始结束时间不能为空");
			return;
		}
		//form.getParams().put("status", "21");

		try {
			response.setHeader(
					"Content-Disposition",
					"attachment;filename="
							+ new String(
									("提现明细" + form.getStartDate() + "-" + form
											.getEndDate()).getBytes(),
									"ISO-8859-1"));
			// 设定输出文件头
			response.setContentType("application/vnd.ms-excel;charset=UTF-8");// 定义输出类型
			// 通过response的输出流把工作薄的流发送浏览器形成文件
			OutputStream outStream = response.getOutputStream();
			exportService.exportSucceseWithdraw(outStream, form);
			return;
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		 //失败或者异常
		JSONObject jsonObject = new JSONObject();
		jsonObject.put("res", -1);
		this.returnJsonObject(response, jsonObject);
		return;
	}
}
